package AbstractFactory.reflect;

import AbstractFactory.IDepartment;
import AbstractFactory.IUser;
import AbstractFactory.MySqlDepartment;
import AbstractFactory.MySqlUser;

import java.lang.reflect.InvocationTargetException;

/**
 * @author duanzhizhou
 * @date 2024/10/14
 */
public class DataAccess {
    
    private static String driverName = "MySql";
    
    public static String getDriverName() {
        return driverName;
    }
    
    public static void setDriverName(String driverName) {
        DataAccess.driverName = driverName;
    }
    
    public static IUser createUser() {
        try {
            return (IUser) Class.forName("AbstractFactory." + driverName + "User").getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public static IDepartment createIDepartment() {
        try {
            return (IDepartment) Class.forName("AbstractFactory." + driverName + "Department").getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    }
}
